package com.example.jetpacktest.activity

import android.os.Bundle
import android.os.PersistableBundle
import android.provider.Settings
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import com.example.jetpacktest.MyApplication
import com.example.jetpacktest.room.bean.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flatMap
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.launch

class RoomTest001Activity: AppCompatActivity() {

    private val TAG: String = this.javaClass.simpleName

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        test008()
    }

    private fun test001(){

        GlobalScope.launch {
            val userDao = MyApplication.getDBInstance().userDao()
            val u1 = User(null, "zhang", "san")
            userDao.addUser(u1)
        }
        //CoroutineScope()
    }


    private fun test002(){
        GlobalScope.launch {
            val userDao = MyApplication.getDBInstance().userDao()
            val users = userDao.getAllUsers()
            users.forEach {
                Log.d(TAG, "$it")
                //println("$it")
            }
        }
    }

    private fun test003(){
        GlobalScope.launch {
            val userDao = MyApplication.getDBInstance().userDao()
            /*userDao.getFullNames().forEach {
                Log.d(TAG, "$it")
            }*/
            userDao.getUsersByIds(listOf(2)).forEach {
                Log.d(TAG, "$it")
            }
        }
    }


    private fun test004(){
        GlobalScope.launch {
            val departmentDao = MyApplication.getDBInstance().departmentDao()
            val employeeDao = MyApplication.getDBInstance().employeeDao()
            val dList = arrayOf<Department>(Department(name = "财务部"), Department(name = "生产部"))
            val eList = arrayOf<Employee>(Employee(name = "zhangsan", age = 15, salary = 2800.0, departmentId = 1),
                Employee(name = "lisi", age = 35, salary = 3800.0, departmentId = 1),
                Employee(name = "wangwu", age = 45, salary = 5900.0, departmentId = 2))
            departmentDao.addDepartment(*dList)
            employeeDao.addEmployee(*eList)
        }
    }

    private fun test005(){
        GlobalScope.launch {
            val employeeDao = MyApplication.getDBInstance().employeeDao()
            /*employeeDao.getAllEmployeeDepartmentInfos().forEach {
                Log.d(TAG, "$it")
            }*/
            employeeDao.getAllEmployees().forEach {
                Log.d(TAG, "$it")
            }
        }
    }


    private fun test006(){
        GlobalScope.launch {
            val employeeDao = MyApplication.getDBInstance().employeeDao()
            employeeDao.realTimeGetAllEmployees().flatMapConcat { list ->
                list.asFlow()
            }.collect { employee ->
                Log.d(TAG, "flow收集到的......$employee")
            }
        }
    }


    private fun test007(){
        GlobalScope.launch {
            val aUserDao = MyApplication.getDBInstance().aUserDao()
            val aLibDao = MyApplication.getDBInstance().aLibraryDao()
            /*val aUsers = listOf<AUser>(AUser(userName = "zhangsan", age = 13),
                AUser(userName = "lisi", age = 22),
                AUser(userName = "wangwu", age = 37))
            val libs = arrayOf<ALibrary>(ALibrary(ownerId = 1, libName = "lib001"),
                ALibrary(ownerId = 2, libName = "lib002"),
                ALibrary(ownerId = 3, libName = "lib003"))
            aUserDao.addAUser(*aUsers.toTypedArray())
            aLibDao.addALibrary(*libs)*/
            aUserDao.getAUserLibrary().forEach {
                Log.d(TAG, "$it")
            }

        }
    }


    private fun test008(){
        GlobalScope.launch {
            val aUserDao = MyApplication.getDBInstance().aUserDao()
            val playListDao = MyApplication.getDBInstance().playListDao()
            /*val aUsers = listOf<AUser>(AUser(userName = "zhangsan", age = 13),
                AUser(userName = "lisi", age = 22),
                AUser(userName = "wangwu", age = 37))
            val playLists = listOf<PlayList>(PlayList(playListName = "song1", creatorId = 2),
                PlayList(playListName = "song2", creatorId = 2),
                PlayList(playListName = "song3", creatorId = 2),
                PlayList(playListName = "song4", creatorId = 2))
            aUserDao.addAUser(*aUsers.toTypedArray())
            playListDao.addPlayList(*playLists.toTypedArray())*/
            aUserDao.getAUserPlayList().forEach {
                Log.d(TAG, "$it")
            }
        }
    }


}